﻿Imports Microsoft.VisualBasic

Public Class FctrClass
#Region "1"
    Shared Function GetFromToFundCenter(ByVal fromfctr As String, ByVal tofctr As String, ByVal users As String, ByVal planyear As Integer) As String
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        dt = adap.GetDataByFromTo(fromfctr, tofctr, users, planyear)

        Dim result As String = "'0'"

        For Each row As BasicData.FCTR_Child2Row In dt
            result += "," & "'" & row.FundCenterCode & "'"
        Next









        Return result
    End Function
    Shared Function GetFromToFundCenterReport(ByVal fromfctr As String, ByVal tofctr As String, ByVal planyear As Integer) As String
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        If fromfctr <> "" Then
            sql = " AND fctr_view.FundCenterCode >= '" + fromfctr + "' "
        End If
        If tofctr <> "" Then
            sql += " AND fctr_view.FundCenterCode <= '" + tofctr + "' "
        End If
        sql += " AND fctr_view.PlanYear = '" + planyear.ToString() + "' "
        sql += " group by fctr_view.FundCenterCode "
        sql += " order by fctr_view.FundCenterCode "
        adap.GetDataByFilterReport(dt, sql)
        'dt = adap.GetDataByFromToReport(fromfctr, tofctr, planyear)
        Dim result As String = "'0'"
        For Each row As BasicData.FCTR_Child2Row In dt
            result += "," & "'" & row.FundCenterCode & "'"
        Next
        Return result
    End Function
    Shared Function GetSubFundCenter(ByVal fctr_code As String, ByVal users As String, ByVal planyear As Integer, ByVal prefilter As String) As String

        Dim gpath As String
        If fctr_code = "0" Then
            gpath = ""
        Else
            Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
            Dim Hdatatable As New FundCenterParent.FctHDataTable
            Hdatatable = Hadapter.GetDataByFctr(fctr_code, planyear)
            For Each row As FundCenterParent.FctHRow In Hdatatable
                gpath = row.grouppath
            Next
        End If
        Dim Hadapter2 As New FundCenterParentTableAdapters.FctH2TableAdapter
        Dim Hdatatable2 As New FundCenterParent.FctH2DataTable
        Dim sql As String = ""
        sql += " SELECT  Fcth.FundCenter_FundCenterCode FROM FctH , fctr_view  "
        sql += " WHERE Fcth.Fundcenter_FundcenterCode = fctr_view.Fundcentercode AND Grouppath like '" & gpath & "%' "
        sql += " AND UserName = '" & users & "' "
        sql += " AND Fcth.PlanYear = " & planyear & " And Fctr_view.PlanYear = " & planyear
        If prefilter.Trim <> "" Then
            sql += " AND FundCenter_FundcenterCode IN (" & prefilter & ")"

        End If
        'Hadapter2.GetDataByFilter(Hdatatable2, sql)
        'Dim result As String = ""
        'For Each row As FundCenterParent.FctH2Row In Hdatatable2

        '    result += "'" & row.FundCenter_FundCenterCode & "'" & ","




        'Next
        'If Trim(result) <> "" Then
        '    result = Left(result, Len(result) - 1)
        'End If

        Return sql
    End Function
    Shared Function GetSubFundCenterrReport(ByVal fctr_code As String, ByVal users As String, ByVal planyear As Integer, ByVal prefilter As String) As String

        Dim gpath As String
        If fctr_code = "0" Then
            gpath = ""
        Else
            Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
            Dim Hdatatable As New FundCenterParent.FctHDataTable
            Hdatatable = Hadapter.GetDataByFctr(fctr_code, planyear)
            For Each row As FundCenterParent.FctHRow In Hdatatable
                gpath = row.grouppath
            Next
        End If
        Dim Hadapter2 As New FundCenterParentTableAdapters.FctH2TableAdapter
        Dim Hdatatable2 As New FundCenterParent.FctH2DataTable
        Dim sql As String = ""
        sql += " SELECT  Fcth.FundCenter_FundCenterCode FROM FctH , fctr_view  "
        sql += " WHERE Fcth.Fundcenter_FundcenterCode = fctr_view.Fundcentercode AND Grouppath like '" & gpath & "%' "
        sql += " AND Fcth.PlanYear = " & planyear & " And Fctr_view.PlanYear = " & planyear
        If prefilter.Trim <> "" Then
            sql += " AND FundCenter_FundcenterCode IN (" & prefilter & ")"

        End If
        Return sql
    End Function
    Shared Function GetLevelFromOrg(ByVal OrgLevel As String) As Integer
        Dim result As Integer
        Select Case Val(Left(OrgLevel, 1))
            Case 1
                result = 1
            Case 2
                result = 2
            Case 3
                result = 3
            Case 4
                result = 4
            Case 5
                result = 5
            Case Else
                result = 1
        End Select
        Return result
    End Function

    Shared Function GetSubFundCenterLevel(ByVal fctr_code As String, ByVal users As String, ByVal planyear As Integer, ByVal prefilter As String, ByVal level As Integer) As String

        Dim result As String

        result = "'" & fctr_code & "'"

        Dim gpath As String
        Dim Hadapter As New FundCenterParentTableAdapters.FctHlevelTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHlevelDataTable
        Hdatatable = Hadapter.GetDataByFctr(fctr_code, planyear)

        If fctr_code = "0" Then
            gpath = "__"
        Else
            For Each row As FundCenterParent.FctHlevelRow In Hdatatable
                If GetLevelFromOrg(row.OrgLevel) <= level Then
                    gpath = row.grouppath
                End If
            Next
            If Hdatatable.Count = 0 Then
                result = ""
            End If
        End If

        GetChild(gpath, result, users, planyear, prefilter) '0 ตัวแรก id ตัว root

        Return result
    End Function

    Shared Sub GetChild(ByVal gpath As String, ByRef result As String, ByVal users As String, ByVal planyear As Integer, ByVal prefilter As String)
        Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHDataTable
        'Hdatatable = Hadapter.GetDataByUserLikeGpath(gpath & "__", users, planyear)
        Dim Hadapter2 As New FundCenterParentTableAdapters.FctH2TableAdapter
        Dim Hdatatable2 As New FundCenterParent.FctH2DataTable

        Dim pathadd As String = "__"
        'For i = 1 To 4
        Dim fullpath As String = gpath & pathadd
        If prefilter.Trim = "" Then
            Hdatatable = Hadapter.GetDataByUserLikeGpath(fullpath, users, planyear)
            'If Hdatatable.Count > 0 Then
            '    Exit For
            'End If
            'pathadd += "__"

        Else

            Dim sql As String = ""

            sql += "SELECT Description, FundCenter_FundCenterCode, ParentFundCenterHierarchy_Id, grouppath, pathname, Fcth.planyear FROM FctH, fctr_view "
            sql += " WHERE 1 = 1 "
            sql += " AND Fcth.fundcenter_fundcenterCode = fctr_view.fundcenterCode "
            sql += " AND grouppath like '" & fullpath & "'"
            sql += " AND FundCenter_FundcenterCode IN (" & prefilter & ")"
            sql += " AND USERNAME = '" & users & "'" & " AND Fcth.planyear = " & planyear
            sql += " AND fctr_view.planyear = " & planyear

            Hadapter2.GetDataByFilter(Hdatatable2, sql)
            'If Hdatatable2.Count > 0 Then
            '    Exit For
            'End If
            'pathadd += "__"

        End If

        'Next
        If prefilter.Trim = "" Then
            For Each row As FundCenterParent.FctHRow In Hdatatable

                result += "," & "'" & row.FundCenter_FundCenterCode & "'"

                If Len(row.grouppath) < 10 Then
                    GetChild(row.grouppath, result, users, planyear, prefilter)
                End If


            Next
        Else
            For Each row As FundCenterParent.FctH2Row In Hdatatable2

                result += "," & "'" & row.FundCenter_FundCenterCode & "'"

                If Len(row.grouppath) < 10 Then
                    GetChild(row.grouppath, result, users, planyear, prefilter)
                End If


            Next
        End If

    End Sub

    Shared Sub GetChildLevel(ByVal gpath As String, ByRef result As String, ByVal users As String, ByVal planyear As Integer, ByVal prefilter As String, ByVal level As Integer)
        Dim Hadapter As New FundCenterParentTableAdapters.FctHlevelTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHlevelDataTable

        Dim pathadd As String = "__"
        'For i = 1 To 4
        Dim fullpath As String = gpath & pathadd
        Hdatatable = Hadapter.GetDataByUserLikeGpath(fullpath, users, planyear)
        'If Hdatatable.Count > 0 Then
        '    Exit For
        'End If
        'pathadd += "__"
        'Next

        For Each row As FundCenterParent.FctHlevelRow In Hdatatable
            If GetLevelFromOrg(row.OrgLevel) <= level Then
                result += "," & "'" & row.FundCenter_FundCenterCode & "'"
                If Len(row.grouppath) < 10 Then
                    GetChildLevel(row.grouppath, result, users, planyear, prefilter, level)
                End If
            End If
        Next

    End Sub
#End Region


    Shared Function CheckLevel(ByVal fctr As String, ByVal planyear As Integer, level As Integer) As Boolean
        Dim result As Boolean
        Dim org As String
        Dim adap As New FundCenterParentTableAdapters.FundCenterHierarchiesTableAdapter
        org = adap.GetOrgLevel(fctr, planyear)
        If GetLevelFromOrg(org) <= level Then
            result = True
        Else
            result = False
        End If
        Return result
    End Function

#Region "2"
    Shared Function GetFromToFundCenter2(ByVal fromfctr As String, ByVal tofctr As String, ByVal users As String, ByVal prefilter As String) As String
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable

        Dim sql = ""
        sql += " WHERE 1 = 1 "
        sql += " AND fctr_view.FundCenterCode BETWEEN " & fromfctr & " AND " & tofctr & " "
        sql += " AND fctr_view.UserName = " & users & " "
        sql += " AND fctr_view.FundCenterCode IN (" & prefilter & ") "

        adap.GetDataByFilter(dt, sql)

        Dim result As String = "'0'"

        For Each row As BasicData.FCTR_Child2Row In dt
            result += "," & row.FundCenterCode
        Next









        Return result
    End Function
    Shared Function GetSubFundCenter2(ByVal fctr_code As String, ByVal users As String, ByVal prefilter As String) As String

        'Dim result As String

        'result = fctr_code








        'GetChild2(fctr_code, result, users) '0 ตัวแรก id ตัว root
        'Return result
    End Function

    Shared Sub GetChild2(ByVal parent_id As String, ByRef result As String, ByVal users As String, ByVal prefilter As String, ByVal planyear As Integer)
        Dim showdata As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim showd As BasicData.FCTR_Child2DataTable
        showd = showdata.GetChild(parent_id, users, planyear)

        Dim sql = ""
        sql += " WHERE 1 = 1 "
        sql += " AND fctr_view.ParentFundcenter = " & parent_id & " "
        sql += " AND fctr_view.FundCenterCode <> " & parent_id & " "
        sql += " AND fctr_view.UserName = " & users & " "
        sql += " AND fctr_view.FundCenterCode IN (" & prefilter & ") "

        showdata.GetDataByFilter(showd, sql)

        For Each shownameRow As BasicData.FCTR_Child2Row In showd

            result += "," & shownameRow.FundCenterCode


            ' GetChild(shownameRow.FundCenterCode, result, users)

        Next



    End Sub


    Shared Function GetUserFundCenter(ByVal users As String, ByVal planyear As Integer) As String
        Dim adap As New BasicDataTableAdapters.FCTR_Child3TableAdapter
        Dim dt As New BasicData.FCTR_Child3DataTable
        dt = adap.GetData(users, planyear)

        Dim result As String = "'0'"

        For Each row As BasicData.FCTR_Child3Row In dt
            result += "," & "'" & row.FundCenterCode & "'"
        Next









        Return result
    End Function

    Shared Function GetUserFundCenterFromTo(ByVal users As String, ByVal planyear As Integer, ByVal fromfctr As String, ByVal tofctr As String) As String
        Dim adap As New BasicDataTableAdapters.FCTR_Child3TableAdapter
        Dim dt As New BasicData.FCTR_Child3DataTable
        dt = adap.GetData(users, planyear)
        Dim result As String = "'0'"
        Dim sql As String = "1=1 "
        If fromfctr <> "" Then
            sql += "AND FundCenterCode >= '" & fromfctr & "'"
        End If
        If tofctr <> "" Then
            sql += "AND FundCenterCode <= '" & tofctr & "'"
        End If
        Dim dr1() As System.Data.DataRow
        If sql <> "" Then
            dr1 = dt.Select(sql)
        End If
        For Each row As System.Data.DataRow In dr1
            result += "," & "'" & row.Item("FundCenterCode") & "'"
        Next
        Return result
    End Function

    Shared Function GetUserFundCenter(ByVal planyear As Integer) As String
        Dim adap As New BasicDataTableAdapters.FCTR_Child3TableAdapter
        Dim dt As New BasicData.FCTR_Child3DataTable
        dt = adap.GetDataByPlanYear(planyear)
        Dim result As String = "'0'"
        For Each row As BasicData.FCTR_Child3Row In dt
            result += "," & "'" & row.FundCenterCode & "'"
        Next
        Return result
    End Function
#End Region

#Region "root"
    Shared Function GetRoot(user_name As String, plan_year As Integer) As String
        Dim result As String = ""
        Dim FctList As String = ""
        Dim adapter2 As New fctrDSTableAdapters.fcth_viewTableAdapter
        Dim dt2 As New fctrDS.fcth_viewDataTable

        'dt2 = adapter2.GetData(user_name, plan_year)
        Dim sql As String
        sql = " order by Fcth.grouppath "
        adapter2.GetDataByFilter(dt2, sql, user_name, plan_year)
        For Each row As fctrDS.fcth_viewRow In dt2
            FctList += "#" & row.grouppath + ","
        Next
        For Each row2 As fctrDS.fcth_viewRow In dt2

            If Len(row2.grouppath) = 2 Then
                result += row2.grouppath & ","
            Else
                Dim temp As String = row2.grouppath

                 
                Dim exist As Integer = 0
                While Len(temp) > 2
                    temp = Left(temp, Len(temp) - 2)
                    If InStr(FctList, "#" & temp & ",") > 0 Then
                        exist = 1
                        Exit While
                    End If
                End While

                If exist = 0 Then
                    result += row2.grouppath & ","
                End If

            End If

        Next
        If result.Length > 0 Then
            result = Left(result, Len(result) - 1)
        End If

        Return result
    End Function

    Shared Function GetRootPrefil(user_name As String, plan_year As Integer, prefil As String) As String
        Dim result As String = ""
        Dim FctList As String = ""
        Dim adapter2 As New fctrDSTableAdapters.fcth_viewTableAdapter
        Dim dt2 As New fctrDS.fcth_viewDataTable
        Dim sql = ""
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") AND Fcth.FundCenter_FUndCenterCode IN (" & prefil & ")"
        End If
        sql += " order by Fcth.grouppath "
        adapter2.GetDataByFilter(dt2, sql, user_name, plan_year)

        For Each row As fctrDS.fcth_viewRow In dt2
            FctList += "#" & row.grouppath + ","
        Next
        For Each row2 As fctrDS.fcth_viewRow In dt2

            If Len(row2.grouppath) = 2 Then
                result += row2.grouppath & ","
            Else
                Dim temp As String = row2.grouppath


                Dim exist As Integer = 0
                While Len(temp) > 2
                    temp = Left(temp, Len(temp) - 2)
                    If InStr(FctList, "#" & temp & ",") > 0 Then
                        exist = 1
                        Exit While
                    End If
                End While

                If exist = 0 Then
                    result += row2.grouppath & ","
                End If
            End If

        Next
        If result.Length > 0 Then
            result = Left(result, Len(result) - 1)
        End If
        Return result
    End Function
#End Region
End Class
